package com.sunlong.jiangwei.web;


import com.sunlong.jiangwei.entity.JwOrder;
import com.sunlong.jiangwei.entity.result.Result;
import com.sunlong.jiangwei.service.IJwOrderService;
import com.sunlong.jiangwei.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 孙龙
 * @since 2021-02-19
 */
@RestController
@RequestMapping("/jiangwei/jw-order")
public class JwOrderController {

    @Autowired
    private IJwOrderService orderService;

    /**
     * 购买一个或者多个商品，生成订单
     * @param map
     * @return
     */
    @PostMapping("/add")
    public Result<Void> add(@RequestBody Map<String, Object> map){
        Map<String,String> errorMap = orderService.add(map);
        if (errorMap.containsKey("existError")){
            return ResultUtil.error(errorMap.get("errorMsg"));
        }
        return ResultUtil.success();
    }

    @PostMapping("/list")
    public Result<List<JwOrder>> list(@RequestBody Map<String, Object> map){
        List<JwOrder> jwOrders = orderService.list(map);
        return ResultUtil.data(jwOrders);
    }

    @PostMapping("/status")
    public Result<Void> status(@RequestBody Map<String, Object> map){
        orderService.status(map);
        return ResultUtil.success();
    }
}
